//
//  HYSearchGameViewController.m
//  HaiYue
//
//  Created by skyline on 2018/1/31.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYSearchGameViewController.h"
#import "HYSearchGameCell.h"
#import "HYSearchGameHeader.h"
@interface HYSearchGameViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) HYAlertView *alertView;
@property (nonatomic, strong) HYIndex *index;
@property (nonatomic, strong) NSArray *cityArray;
@property (nonatomic, strong) NSArray *indexArray;
@end

@implementation HYSearchGameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.search.placeholder = @"搜索要查找的游戏";
    self.collectionView.hidden = NO;
    
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:[self layout]];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[HYSearchGameCell class] forCellWithReuseIdentifier:@"HYSearchGameCell"];
        [_collectionView registerClass:[HYSearchGameHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HYSearchGameHeader"];
        [self.view addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.bottom.equalTo(self.view);
            make.left.equalTo(self.view);
            make.right.equalTo(self.index.mas_left).with.offset(0);
        }];
    }
    return _collectionView;
}

- (UICollectionViewFlowLayout *)layout
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - self.index.bounds.size.width) / 2.0 - GAP, HYOnlineCellIMG_Width + GAP);
    layout.sectionHeadersPinToVisibleBounds = YES;
    //    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, HYSearchGameCellHeaderHight);//设置header的size
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    return layout;
}

- (HYAlertView *)alertView
{
    if (!_alertView)
    {
        _alertView = [[HYAlertView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    }
    return _alertView;
}

- (HYIndex *)index
{
    if (!_index)
    {
        _index = [[HYIndex alloc] init];
        [_index addTarget:self action:@selector(actionForScrolling:) forControlEvents:UIControlEventValueChanged];
        [_index addTarget:self action:@selector(actionForScrolled:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_index];
        [_index mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.view);
            make.top.equalTo(self.view);
            make.bottom.equalTo(self.view);
        }];
    }
    return _index;
}
- (NSArray *)cityArray
{
    if(!_cityArray)
    {
        NSArray *array = @[@"北京",@"上海",@"广州",@"阿坝",@"阿克苏",@"安康",@"安阳",@"澳门",@"北京",@"白城",@"白山",@"包头",@"宝鸡",@"保定",@"滨州",@"重庆",@"成都",@"长沙",@"长春",@"沧州",@"赤峰",@"大连",@"东莞",@"达州",@"大理",@"大同",@"大兴安岭",@"丹东",@"东营",@"鄂尔多斯",@"鄂州",@"恩施", @"佛山",@"福州",@"抚顺",@"阜新",@"阜阳", @"广州",@"贵阳",@"桂林",@"甘南",@"格尔木",@"广安",@"广元",@"杭州",@"海口",@"哈尔滨",@"合肥",@"哈密",@"海北",@"海东",@"海西",@"海南",@"邯郸",@"菏泽",@"鹤岗",@"黄山", @"济南",@"锦州",@"吉林",@"济宁",@"嘉兴",@"嘉峪关",@"金华",@"荆门",@"荆州",@"酒泉",@"景德镇", @"昆明",@"喀什",@"开封",@"克拉玛依", @"洛阳",@"拉萨",@"兰州",@"莱芜",@"廊坊",@"乐山",@"丽江",@"连云港",@"辽阳",@"临汾",@"临沂",@"六盘水", @"茂名",@"马鞍山",@"牡丹江", @"南京",@"宁波",@"南昌",@"南宁",@"南通",@"宁德", @"攀枝花",@"盘锦",@"平顶山",@"普洱", @"青岛",@"齐齐哈尔",@"黔南",@"秦皇岛",@"庆阳",@"琼海",@"泉州", @"日喀什",@"日照", @"上海",@"深圳",@"沈阳",@"石家庄",@"苏州",@"三门峡",@"三亚",@"汕头",@"绍兴",@"十堰",@"石河子",@"松原",@"天津",@"唐山",@"台湾",@"太原",@"泰州",@"泰安",@"通辽",@"吐鲁番", @"武汉",@"无锡",@"潍坊",@"乌鲁木齐",@"威海",@"五指山",@"西安",@"厦门",@"徐州",@"西沙",@"仙桃",@"咸阳",@"孝感",
                           //                           @"银川",@"雅安",@"烟台",@"延安",@"盐城",@"扬州",@"阳江",@"宜宾",@"宜昌",@"玉树",
                           @"郑州",@"珠海",@"淄博",@"漳州",@"张家口",@"驻马店",@"遵义"];
        _cityArray= [array orderWithFirstCharacter];
        NSLog(@"************");
    }
    return _cityArray;
}


- (NSArray *)indexArray
{
    if(!_indexArray)
    {
        _indexArray = [NSArray upperCharacters];
    }
    return _indexArray;
}

#pragma mark button method
- (void)actionForScrolling:(HYIndex *)index
{
    [self.alertView showMessage:self.indexArray[index.currentIndex] hidden:NO];
    NSLog(@"index %d",index.currentIndex);
}

- (void)actionForScrolled:(HYIndex *)index
{
    
    [self.alertView showMessage:self.indexArray[index.currentIndex] hidden:YES];
    
    //遍历所有大字母，如果存在，跟索引选中的字母，就自动滚到相应位置
    for (int i = 0; i < self.cityArray.count; i++)
    {
        NSString *title = [[self.cityArray[i] allKeys] firstObject];
        if ([title isEqualToString:self.indexArray[index.currentIndex]])
        {
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
//            [self.collectionView setContentOffset:CGPointMake(0, self.collectionView.contentOffset.y - HYSearchGameCellHeaderHight) animated:NO];
        }
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[[[self.cityArray objectAtIndex:section] allValues]firstObject] count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.cityArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HYSearchGameCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HYSearchGameCell" forIndexPath:indexPath];
    if (!cell)
    {
        cell = [[HYSearchGameCell alloc] init];
        
    }
    return cell;
}

#pragma mark - 显示headerview
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *view = nil;
    
    
    if (kind == UICollectionElementKindSectionHeader)
    {
        HYSearchGameHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HYSearchGameHeader" forIndexPath:indexPath];
        header.title = [[self.cityArray[indexPath.section] allKeys] firstObject];
        view = header;
    }
    else if (kind == UICollectionElementKindSectionFooter)
    {
        
    }
    return view;
}

#pragma mark - UICollectionDelegateFlowLayout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, HYSearchGameCellHeaderHight);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
